package com.warm.pump.module.skd.controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.StringUtils;
import com.warm.pump.module.skd.bean.po.gen.ScheduleInstance;
import com.warm.pump.module.skd.service.IScheduleInstanceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * skd_instance
 */
@Controller
@RequestMapping("/scheduleInstance")
public class ScheduleInstanceController {
    @Autowired
    private IScheduleInstanceService scheduleInstanceService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("scheduleInstance:list")
    public OutJson list(Integer page, Integer limit, ScheduleInstance scheduleInstance) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, scheduleInstance);

        map.put("orderBy", "create_time desc");
        if (StringUtils.isNotEmpty(scheduleInstance.getSidx()) && StringUtils.isNotEmpty(scheduleInstance.getOrder())) {
            map.put("orderBy", scheduleInstance.getSidx() + " " + scheduleInstance.getOrder());
        }
        //查询列表数据
        PageBean pageBean = scheduleInstanceService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("scheduleInstance:info")
    public OutJson info(@PathVariable("id") Long id) {
        ScheduleInstance scheduleInstance = scheduleInstanceService.queryObject(id);

        return OutJson.ok().put("scheduleInstance", scheduleInstance);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("scheduleInstance:save")
    public OutJson save(@RequestBody ScheduleInstance scheduleInstance) {
        scheduleInstanceService.save(scheduleInstance);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("scheduleInstance:update")
    public OutJson update(@RequestBody ScheduleInstance scheduleInstance) {
        scheduleInstanceService.update(scheduleInstance);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("scheduleInstance:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        scheduleInstanceService.deleteBatch(ids);

        return OutJson.ok();
    }

    /**
     * 立即执行任务
     */
    @ResponseBody
    @RequestMapping("/run")
    public OutJson run(@RequestBody Long[] ids) {
        scheduleInstanceService.run(ids);
        return OutJson.ok();
    }


    /**
     * 执行完成
     */
    @ResponseBody
    @RequestMapping("/completedInst")
    public OutJson completedInst(@RequestBody Long[] ids) {
        scheduleInstanceService.completedInst(ids);
        return OutJson.ok();
    }

}
